/**
 * Project Name:filmbComm File Name:FastDFSUtils.java Package
 * Name:com.filmb.comm.file Date:2014年10月30日下午2:32:35 Copyright (c) 2014
 * 
 */
/**
 * @projectName:filmbComm
 * @packageName:com.filmb.comm.file
 * @className:FastDFSUtils.java
 * 
 * @createTime:2014年10月30日-下午2:32:35
 * 
 * 
 */

package com.yn.xr.common.util;

import java.io.File;
import java.io.FileInputStream;

import org.apache.log4j.Logger;
import org.csource.common.IniFileReader;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.ServerInfo;
import org.csource.fastdfs.StorageClient;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;

import com.yn.xr.common.vo.FileVO;
/**
 * @description:FastDFS文件工具类
 * @fileName:FastDFSUtils.java
 * @createTime:2014年10月30日 下午2:32:35
 * @author:肖震
 * @version 1.0.0
 * 
 */
public class FastDFSUtils
{
	
	private static Logger log = Logger.getLogger(FastDFSUtils.class);
	
	
	/**
	 * 
	 * @function:获得文件名称
	 * @param filePath
	 * @return String
	 * @exception
	 * @author:肖震
	 * @since 1.0.0
	 */
	public static String getFileName(String filePath)
	{
		String fileName = null;
		if (filePath != null)
		{
			int index = filePath.replace("\\", "/").lastIndexOf("/");
			fileName = filePath.substring(index);
		}
		return fileName;
	}
	
	/**
	 * 
	 * @function: 获得文件格式
	 * @param fileName
	 * @return String
	 * @exception
	 * @author:肖震
	 * @since 1.0.0
	 */
	public static String getFileFormat(String fileName)
	{
		String fileFormat = null;
		if (fileName != null && fileName.contains("."))
		{
			String[] arry = fileName.split("[.]");
			fileFormat = arry[1];
		}
		return fileFormat;
	}
	
	/**
	 * 
	 * @function:上传文件
	 * @param filePath 文件路径
	 * @return FileVO 返回上传后的文件信息
	 * @throws Exception
	 * @exception
	 * @author:肖震
	 * @since 1.0.0
	 */
	public static FileVO upload(String filePath, String configRootPath,
			String oldFileName, String fileFormat)
		throws Exception
	{
		File file = new File(filePath);
		FileVO fileVo = upload(file, configRootPath, oldFileName, fileFormat);
		return fileVo;
	}
	
	/**
	 * 
	 * @function: 文件上传
	 * @param file
	 * @return FileVO
	 * @throws Exception
	 * @exception
	 * @author:肖震
	 * @since 1.0.0
	 */
	public static FileVO upload(File file, String configRootPath,
			String oldFileName, String fileFormat)
		throws Exception
	{
		boolean isOK = false;
		long uploadTime = 0;
		String groupName = null;
		String newFileName = null;
		long fileSize = 0;
		
		String classPath = configRootPath == null ? new File(FastDFSUtils.class
				.getResource("/").getFile()).getCanonicalPath()
				: configRootPath;
		String configFilePath = classPath + File.separator + "fdfs_client.conf";
		if (new File(configFilePath.replace("%20", " ")).exists())
		{
			System.out.println("配置文件:" + configFilePath);
			
			ClientGlobal.init(configFilePath.replace("%20", " "));
			IniFileReader iniReader = new IniFileReader(configFilePath.replace("%20", " "));
			groupName=iniReader.getStrValue("groupName");
			TrackerClient trackerClient = new TrackerClient();
			TrackerServer trackerServer = trackerClient.getConnection();
			
			StorageServer storageServer = null;
			StorageClient storageClient = new StorageClient(trackerServer,
					storageServer);
			
			NameValuePair[] meta_list = new NameValuePair[3];
			meta_list[0] = new NameValuePair("width", "120");
			meta_list[1] = new NameValuePair("heigth", "120");
			meta_list[2] = new NameValuePair("author", "gary");
			FileInputStream fis = new FileInputStream(file);
			byte[] file_buff = null;
			if (fis != null)
			{
				int len = fis.available();
				file_buff = new byte[len];
				fis.read(file_buff);
			}
			log.info("file length: " + file_buff.length);
			
			StorageServer[] storageServers = trackerClient.getStoreStorages(
					trackerServer, groupName);
			if (storageServers == null)
			{
				log.error("get store storage servers fail, error code: "
						+ storageClient.getErrorCode());
			}
			else
			{
				log.info("store storage servers count: "
						+ storageServers.length);
				for (int k = 0; k < storageServers.length; k++)
				{
					log.info(k
							+ 1
							+ ". "
							+ storageServers[k].getInetSocketAddress()
									.getAddress().getHostAddress()
							+ ":"
							+ storageServers[k].getInetSocketAddress()
									.getPort());
				}
				
			}
			
			long startTime = System.currentTimeMillis();
			/*String[] results = storageClient.upload_file(file_buff, fileFormat,
					meta_list);*/
			String[] results = storageClient.upload_file(groupName,file_buff, fileFormat,
					meta_list);
			uploadTime = System.currentTimeMillis() - startTime;
			log.info("upload_file time used: " + uploadTime + " ms");
			
			if (results == null)
			{
				log.error("upload file fail, error code: "
						+ storageClient.getErrorCode());
				
				throw new Exception("upload file fail, error code: "
						+ storageClient.getErrorCode());
			}
			isOK = true;
//			groupName = results[0];
			newFileName = results[1];
			log.info("group_name: " + groupName + ", remote_filename: "
					+ newFileName);
			log.info(storageClient.get_file_info(groupName, newFileName));
			
			ServerInfo[] servers = trackerClient.getFetchStorages(
					trackerServer, groupName, newFileName);
			if (servers == null)
			{
				log.error("get storage servers fail, error code: "
						+ trackerClient.getErrorCode());
			}
			else
			{
				log.info("storage servers count: " + servers.length);
				for (int k = 0; k < servers.length; k++)
				{
					log.info(k + 1 + ". " + servers[k].getIpAddr() + ":"
							+ servers[k].getPort());
				}
				
			}
		}
		else
		{
			log.error("配置文件不存在！" + configFilePath);
			throw new Exception("配置文件不存在！");
		}
		FileVO fileVo = new FileVO(isOK, groupName, oldFileName, newFileName,
				fileSize, uploadTime, fileFormat);
		return fileVo;
	}
	
	/**
	 * 
	 * @function: 文件下载
	 * @param groupName 组名
	 * @param fileName 文件名称
	 * @return byte[]
	 * @throws Exception
	 * @exception
	 * @author:肖震
	 * @since 1.0.0
	 */
	public static byte[] download(String groupName, String fileName,
			String configRootPath)
		throws Exception
	{
		String classPath = configRootPath == null ? new File(FastDFSUtils.class
				.getResource("/").getFile()).getCanonicalPath()
				: configRootPath;
		String configFilePath = classPath + File.separator + "fdfs_client.conf";
		ClientGlobal.init(configFilePath.replace("%20", " ").replace("\\", "/"));
		TrackerClient trackerClient = new TrackerClient();
		TrackerServer trackerServer = trackerClient.getConnection();
		StorageServer storageServer = null;
		StorageClient storageClient = new StorageClient(trackerServer,
				storageServer);
		byte[] bytes = null;
		bytes = storageClient.download_file(groupName, fileName);
		return bytes;
		
	}
}
